// @Author EthanScriptOn
// @Desc
package starter

import (
	"gitee.com/fatzeng/srf_switch_basic_components/util"
	"gitee.com/fatzeng/srf_switch_config/config_context"
	"gitee.com/fatzeng/srf_switch_config/config_freighter"
	"gitee.com/fatzeng/srf_switch_config/config_parser"
	"gitee.com/fatzeng/srf_switch_config/config_processor_management"
	"gitee.com/fatzeng/srf_switch_config/config_source_loader_management"
)

func Start(ctx *config_context.ConfigInitializationContext) (err error) {
	defer util.HandleErr(&err)
	if err = resourcePreparation(ctx); err != nil {
		return
	}
	if err = config_parser.StartParse(ctx); err != nil {
		return err
	}
	return config_freighter.StartFreight(ctx)
}

func resourcePreparation(ctx *config_context.ConfigInitializationContext) (err error) {
	if err = initSourceLoader(ctx); err != nil {
		return err
	}
	if err = initProcessor(); err != nil {
		return err
	}
	return
}

func initSourceLoader(ctx *config_context.ConfigInitializationContext) (err error) {
	managerInstance, err := config_source_loader_management.GetMainSourceLoaderManager()
	if err = managerInstance.CreateSourceLoader(ctx); err != nil {
		return
	}
	return
}

func initProcessor() error {
	if processorManagerInstance, err := config_processor_management.GetParseProcessorManager(); err != nil {
		return err
	} else {
		if err = processorManagerInstance.CreateProcessor(); err != nil {
			return err
		}
	}
	if processorManagerInstance, err := config_processor_management.GetFreightProcessorManager(); err != nil {
		return err
	} else {
		if err = processorManagerInstance.CreateProcessor(); err != nil {
			return err
		}
	}
	return nil
}
